package com.uuah.server.config.spring.parsers.collection;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

import com.uuah.server.config.spring.parsers.AbstractChildDefinitionParser;
import com.uuah.server.util.spring.SpringXMLUtils;
import com.uuah.utils.StringUtils;

public class AttributeListEntryDefinitionParser extends
		AbstractChildDefinitionParser implements
		IDynamicAttributeDefinitionParser {

	private String setterMethod;
	private String attributeName;

	public AttributeListEntryDefinitionParser(String setterMethod) {
		this(setterMethod, null);
	}

	public AttributeListEntryDefinitionParser(String setterMethod,
			String attributeName) {
		this.setterMethod = setterMethod;
		setAttributeName(attributeName);
	}

	public String getPropertyName(Element element) {
		return setterMethod;
	}

	protected Class getBeanClass(Element element) {
		return ChildListEntryDefinitionParser.ListEntry.class;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}

	protected void parseChild(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder) {
		Attr attribute = element.getAttributeNode(attributeName);
		if (null == attribute || StringUtils.isEmpty(attribute.getNodeValue())) {
			throw new IllegalStateException("No value for " + attributeName
					+ " in " + SpringXMLUtils.elementToString(element));
		}
		String value = attribute.getNodeValue();
		builder.getRawBeanDefinition().setSource(
				new ChildListEntryDefinitionParser.ListEntry(value));
		this.postProcess(parserContext, getBeanAssembler(element, builder),
				element);
	}

}
